using System;
using System.Collections.Generic;
using System.Linq;

namespace Client
{
	public class Collection
	{
		private bool m_Scanning;
		private ScanDirectory m_ScanDir;
		private List<Music> m_musics;
		
		public List<Music> Musics {
			get{return m_musics;}
		}
		
		public int NbFolderInScan {
			get {return (m_ScanDir != null && m_Scanning)? m_ScanDir.NbFolderSCanned : 0;}
		}
		
		public string CurrentfolderScan {
			get {return (m_ScanDir != null && m_Scanning)? m_ScanDir.CurentPath : String.Empty;}
		}
		
		
		public Collection () {
			m_musics = new List<Music>();
		}
		
		public void ScanFolder(string path) {
			m_Scanning = true;
			m_ScanDir = new ScanDirectory(path);
			
			DateTime deb = DateTime.Now;
			AddMusic(m_ScanDir.Run());
			DateTime fin = DateTime.Now;
			
			Console.WriteLine((fin - deb).Seconds);
			
			m_Scanning = false;
		}
		
		
		public List<string> Albums {
			get {
				return (from music in m_musics where music.Album != null select music.Album).Distinct().ToList();
			}
		}
		
		public List<string> Artists {
			get {
				return (from music in m_musics where music.Artist != null select music.Artist).Distinct().ToList();
			}
		}
		
		public List<Music> GetMusicsSelected() {
			return (from music in m_musics where music.IsSelected select music).ToList();
		}
		
		private void AddMusic(List<Music> p_musics) {
			foreach(Music music in p_musics) {
				if(!m_musics.Contains(music))
					m_musics.Add(music);
			}
		}
		
		public void SortCollection() {
			m_musics.Sort(delegate(Music x, Music y) {return x.Title.CompareTo(y.Title);});
		}
	}
}

